<template>
  <div>
    <Dialog :visible="bindLinkVisible" :title="visibleTitle" :footer="false" width="75%" @close="closeBindLink">
      <el-form label-width="80px">
        <el-row :gutter="24">
          <el-col :span="20" style="padding: 0">
            <el-col :span="8" :xs="24">
              <el-form-item :label="$t('渠道编号')">
                <el-input v-model="page.channelCode"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item :label="$t('渠道名称')">
                <el-input v-model="page.channelName"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item :label="$t('APP名称')">
                <el-select style="width: 100%" v-model="page.productName" clearable>
                  <el-option v-for="item in channelProduct" :key="item" :label="item"
                             :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item :label="$t('渠道链接')">
                <el-input v-model="page.channelLink"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item :label="$t('状态')">
                <el-select
                    style="width: 100%"
                    v-model="page.status"
                    clearable
                >
                  <el-option :label="$t('启用')" :value="1"></el-option>
                  <el-option :label="$t('禁用')" :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="4" :xs="24" style="text-align: right">
            <el-button type="primary" @click="getLinkTableData">{{ $t('查询') }}</el-button>
            <el-button @click="resetSearch">{{ $t('重置') }}</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <div style="margin-bottom: 10px;text-align: right">
          <el-button type="primary" v-show="$permiss[897]" @click="addLinkVisible = true">{{ $t('新增链接') }}</el-button>
        </div>
        <Table :table-data="tableData" :table-column="tableColumn" :page="page" @handleSizeChange="(val)=>{
        page.pageSize = val
        getLinkTableData()
      }" @handleCurrentChange="(val)=>{
        page.pageNum = val
        getLinkTableData()
      }">
          <template slot="link" slot-scope="{row}">
            <div style="display: flex">
              <div class="overflow" style="margin-right: 5px" :title="row.channelLink" slot="reference">{{ row.channelLink }}</div>
              <i class="el-icon-document-copy cursor_pointer" v-show="row.channelLink" style="font-size: 18px"
                 @click="copyLink(row.channelLink)"></i>
            </div>
          </template>
          <template slot="status" slot-scope="{row}">
            <el-switch :disabled="!$permiss[894]" @change="changeStatus(row,$event)"
                       :value="row.status === 1"></el-switch>
          </template>
          <template slot="black" slot-scope="{row}">
            <el-switch :disabled="!$permiss[894]" @change="changeBlack(row,$event)"
                       :value="row.blacklist === 1"></el-switch>
          </template>
          <template slot="handle" slot-scope="{row}">
            <el-button type="text" v-show="$permiss[895]" @click="lookConfigVisible = true,configRows = row">
              {{ $t('查看数据配置') }}
            </el-button>
          </template>
        </Table>
      </div>
      <!--      新增渠道链接-->
      <Dialog :appendBody="true" :visible.sync="addLinkVisible" :title="$t('新增渠道链接')" width="40%"
              @cancel="addLinkVisible = false" @close="clearAddLink" @confirm="addLinkConfirm">
        <el-form label-width="80px" :model="addLinkForm" ref="addLinkForm" :rules="rulesAddForm">
          <el-form-item :label="$t('APP名称')" prop="productName">
            <el-select style="width: 100%" v-model="addLinkForm.productName" clearable>
              <el-option v-for="item in channelProduct" :key="item" :label="item"
                         :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('渠道名称')" prop="channelName">
            <el-input v-model="addLinkForm.channelName"/>
          </el-form-item>
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="addLinkForm.remark"/>
          </el-form-item>
          <el-form-item :label="$t('状态')">
            <el-radio-group v-model="addLinkForm.status">
              <el-radio :label="1">{{ $t('启用') }}</el-radio>
              <el-radio :label="0">{{ $t('禁用') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div style="margin-bottom: 10px ">
            <el-link :underline="false" type="primary">{{ $t('转化数据配置') }}</el-link>
          </div>
          <el-form-item :label="$t('PV数')" prop="pv">
            <el-input v-model="addLinkForm.pv" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('UV数')" prop="uv">
            <el-input v-model="addLinkForm.uv" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('注册数')" prop="registered">
            <el-input v-model="addLinkForm.registered" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('下载数')" prop="download">
            <el-input v-model="addLinkForm.download" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('申请数')" prop="loan">
            <el-input v-model="addLinkForm.loan" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('放款数')" prop="payment">
            <el-input v-model="addLinkForm.payment" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-alert
              style="margin-top: 15px"
              :title="$t('转化数据配置时，请注意控制由上至下的百分比关系；')"
              type="info"
              show-icon
              :closable="false">
          </el-alert>
        </el-form>
      </Dialog>
      <!--      查看数据配置-->
      <Dialog append-body :footer="false" :visible.sync="lookConfigVisible" :title="$t('转化数据配置')">
        <el-form label-width="80px">
          <el-form-item :label="$t('PV数')" prop="pv">
            <div style="line-height: 32px">
              {{ configRows.pv || 0}}%
            </div>
          </el-form-item>
          <el-form-item :label="$t('UV数')" prop="uv">
            <div style="line-height: 32px">
              {{ configRows.uv || 0}}%
            </div>
          </el-form-item>
          <el-form-item :label="$t('注册数')" prop="registered">
            <div style="line-height: 32px">
              {{ configRows.registered || 0}}%
            </div>
          </el-form-item>
          <el-form-item :label="$t('下载数')" prop="download">
            <div style="line-height: 32px">
              {{ configRows.download|| 0 }}%
            </div>
          </el-form-item>
          <el-form-item :label="$t('申请数')" prop="loan">
            <div style="line-height: 32px">
              {{ configRows.loan || 0}}%
            </div>
          </el-form-item>
          <el-form-item :label="$t('放款数')" prop="payment">
            <div style="line-height: 32px">
              {{ configRows.payment || 0}}%
            </div>
          </el-form-item>
        </el-form>
      </Dialog>
    </Dialog>
  </div>
</template>

<script>
import qs from "qs";
import {fetchChannelLinkList,selectChannelProduct,addChannelLink,updateLinkStatus} from '@/api/channel'
export default {
  props: {
    bindLinkVisible: Boolean,
    rows: {
      default: function () {
        return {}
      },
      type: Object
    },
    productList: {
      default: function () {
        return []
      },
      type: Array
    }
  },
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      tableData: [],
      tableColumn: [
        {label: this.$t('渠道编号'), prop: 'channelCode', align: 'center', minWidth: 100},
        {label: this.$t('渠道名称'), prop: 'channelName', align: 'center', minWidth: 100},
        {label: this.$t('APP名称'), prop: 'appName', align: 'center', minWidth: 100},
        {label: this.$t('渠道链接'), prop: 'channelLink', slot: 'link', align: 'center', minWidth: 200},
        {label: this.$t('状态'), prop: 'status', slot: 'status', align: 'center', minWidth: 100},
        {label: this.$t('禁用后注册即拉黑'), prop: 'blackList', slot: 'black', align: 'center', minWidth: 120},
        {label: this.$t('创建人'), prop: 'operator', align: 'center', minWidth: 100},
        {label: this.$t('备注'), prop: 'remark', align: 'center', minWidth: 100},
        {label: this.$t('创建时间'), prop: 'inputTime', align: 'center', minWidth: 150},
        {label: this.$t('操作'), slot: 'handle', align: 'center', minWidth: 100, isHide: !this.$permiss[895]},
      ],
      addLinkVisible: false,
      addLinkForm: {
        status: 1
      },
      channelProduct: [],
      rulesAddForm: {
        productName: [
          {required: true, message: this.$t('不能为空')},
        ],
        channelName: [
          {required: true, message: this.$t('不能为空')},
        ],
      },
      lookConfigVisible: false,
      configRows: {}
    }
  },
  computed: {
    visibleTitle() {
      return this.$t('投放公司') + ' - ' + (this.rows.distributorName || '')
    }
  },
  watch: {
    bindLinkVisible(val) {
      if (val) {
        this.getLinkTableData()
        this.getSelect()
      }
    }
  },
  methods: {
    async getLinkTableData() {
      const res = await fetchChannelLinkList({...this.page, distributorId: this.rows.id})
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list
        this.page = {
          ...this.page,
          total
        }
      }
    },
    async getSelect() {
      this.channelProduct = await this.getChannelProduct()
    },
    // 获取渠道产品名称
    async getChannelProduct() {
      const res = await selectChannelProduct()
      if (res.code === 1) return res.data
      return []
    },
    // 添加渠道链接
    addLinkConfirm() {
      this.$refs.addLinkForm.validate(async v => {
        if (v) {
          const {download, loan, payment, pv, registered, uv} = this.addLinkForm
          const params = {
            ...this.addLinkForm,
            download: parseInt(download) || undefined,
            loan: parseInt(loan) || undefined,
            payment: parseInt(payment) || undefined,
            pv: parseInt(pv) || undefined,
            registered: parseInt(registered) || undefined,
            uv: parseInt(uv) || undefined,
            distributorName:this.rows.distributorName
          }
          const res = await addChannelLink(qs.stringify({
            distributorId: this.rows.id,
            ...params
          }))
          if (res.code === 1) {
            this.$message.success(this.$t('新增成功'))
            this.getLinkTableData()
            this.addLinkVisible = false
          }
        }
      })
    },

   async changeBlack({id, blacklist}){
      const res = await updateLinkStatus(qs.stringify({
        blacklist:blacklist === 1? 0 :1,
        id,
      }))
     if (res.code === 1) {
       this.$message.success(this.$t('操作成功'))
       this.getLinkTableData()
     }
    },
    // 拷贝链接
    copyLink(text) {
      let input = document.createElement("input");
      input.value = text
      document.body.appendChild(input);
      input.select()
      document.execCommand('copy')
      this.$message.success(this.$t('复制成功'))
      document.body.removeChild(input);
    },
    // 切换状态
    async changeStatus({id, status}) {
      const res = await updateLinkStatus(qs.stringify({
        id,
        status: status === 1 ? 0 : 1
      }))
      if (res.code === 1) {
        this.$message.success(this.$t('操作成功'))
        this.getLinkTableData()
      }
    },
    closeBindLink() {
      this.$emit('update:bindLinkVisible', false)
    },
    resetSearch() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.getLinkTableData()
    },
    clearAddLink() {
      this.page = {
          pageNum: 1,
          pageSize: 10,
          total: 0
      }
      this.addLinkForm = {
        status: 1
      }
      this.$refs.addLinkForm.resetFields()
    }
  },
  beforeDestroy() {
    this.$emit('update:bindLinkVisible', false)
  }
}
</script>

<style scoped>

</style>
